package edu.njpi.vueajax.controller;

import com.google.gson.Gson;
import edu.njpi.vueajax.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin 
@Controller
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/getAllProducts")
    @ResponseBody
    public String getAllProducts(){
        return  new Gson().toJson(productService.getAllProducts());
    }
    
    @PostMapping("/save")
    @ResponseBody
    public String saveProduct(@RequestParam String name){
        Map<String,String> result=new HashMap();
        if(productService.saveProduct(name)){
            result.put("result","ok");
            result.put("msg","product保存成功!");
        }else{
            result.put("result","false");
            result.put("msg","product保存失败!");
        }
        return  new Gson().toJson(result);
    }

    @PostMapping("/delete")
    @ResponseBody
    public String deleteProductById(@RequestParam int prodId){
        Map<String,String> result=new HashMap();
        if(productService.deleteProductById(prodId)){
            result.put("result","ok");
            result.put("msg","product删除成功!");
        }else{
            result.put("result","false");
            result.put("msg","product删除失败!");
        }
        return  new Gson().toJson(result);
        
    }
}
